package com.udl.emailfs.main;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import com.udl.emailfs.filedirectory.TypeDescriptorFS;
import com.udl.emailfs.service.EmailFDS;
import com.udl.emailfs.utils.ConstantsFS;

public class TestEmailFs {

	public static final int CBufferSize = 10000;
	
	private static FileOutputStream fos;
	private static byte[] data, dataOut;
	private static String pathAbsolute =""; 
	
	private static EmailFDS emailFS;
	
	public static void main(String[] args) {
		
		try{
			emailFS = new EmailFDS(); //(InterfaceRemoto) Naming.lookup(ConstantsFS.OBJECT_REMOTE);
			
			pathAbsolute = new File("").getAbsolutePath();
			pathAbsolute = pathAbsolute.replace("\\", "/");
			fos = new FileOutputStream(pathAbsolute+"/out.txt"); 
			
			data = new byte[ConstantsFS.BYTELEN];
			dataOut = new byte[0];
			//Funci�n para formatear el sistema de archivos.
			emailFS.reset();		
			
			// Pruebas.
			//rmdir("/Nando");
			mkdir("/Nando");
			if (copyL2R("/home/nando2/temp/f3.txt","/Nando/f3r.txt"))
				System.out.println("Fichero "+"~/temp/f3.txt"+" copiado correctamente.");
			System.gc();
			
			if (copyL2R("/home/nando2/tmp/mpich2-1.3.2.tar.gz","/Nando/mpich2-1.3.2.tar.r.gz"))
				System.out.println("Fichero "+"~/temp/f3.txt"+" copiado correctamente.");
			System.gc();
		}
		catch (Exception e1) {
			e1.printStackTrace();
		}
	}

	// Crear directorio remoto
	public static int mkdir(String path)
	{
		TypeDescriptorFS fd;
		
		try {
			fd = emailFS.create(path, ConstantsFS.DIRECTORY);
			emailFS.close(fd);
			return 0;
		}
		catch (Exception e1) {
			e1.printStackTrace();
			return -1;
		}
	}
	
	// Eliminar directorio remoto
	public static int rmdir(String path)
	{
		TypeDescriptorFS remoteDir;
		
		try {
			// Abrir directorio remoto.
			remoteDir = emailFS.open(path, "");
			
			if (emailFS.delete(remoteDir))
				System.out.println("Directorio "+path+ " borrado.");
				
			emailFS.close(remoteDir);
			return 0;
		}
		catch (Exception e1) {
			e1.printStackTrace();
			return -1;
		}
	}
	
	// Copiar fichero local en ruta
	public static boolean copyL2R(String localFilePath, String remoteFilePath)
	{
		TypeDescriptorFS remoteFile=null;
		FileInputStream inputFile=null;
		int bytes_read, total=0;
		byte[] buffer= new byte[CBufferSize];
		
		try {
			// Abrir fichero local.
			inputFile = new FileInputStream(localFilePath); 
			
			// Abrir fichero remoto.
			remoteFile = emailFS.open(remoteFilePath, "");
			
			if (remoteFile==null)
				remoteFile = emailFS.create(remoteFilePath,  ConstantsFS.FILE);
			
			if (remoteFile!=null)
			{
		        while((bytes_read=inputFile.read(buffer))>0)
		        {
		        	emailFS.write(remoteFile,buffer,bytes_read);
		        	total+=bytes_read;
		            System.out.println("Escritos "+bytes_read+ " bytes. Total: "+total+".");
		        }			
			}
			else return false;
		}
		catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {                    
	             if (inputFile != null) inputFile.close();     
	             if (remoteFile != null) emailFS.close(remoteFile);    
	             return true;
			}
			catch (Exception e2) {
				e2.printStackTrace();
			}
		}
		return false;
	}

/*
	//Copiar fichero remoto (SAD) a fichero local
	public static boolean copyR2L(String remoteFilePath, String localFilePath)
	{
		TypeDescriptorFS remoteFile=null;
		FileOutputStream outputFile=null;
		int bytes_write;
		byte[] buffer= new byte[CBufferSize];
		
		try {
			// Abrir fichero local.
			outputFile = new FileOutputStream(localFilePath); 
			
			// Abrir fichero remoto.
			remoteFile = emailFS.open(remoteFilePath, "");
			
			if (remoteFile!=null)
			{
		        while((bytes_read=inputFile.read(buffer))>0)
		        {
		        	emailFS.write(remoteFile,buffer,bytes_read);
		        	
		        	outputFile.write(buffer,bytes_write);
		        	
		            System.out.println("Escritos "+bytes_read+ " bytes.");
		        }			
			}
			else return false;
		}
		catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {                    
	             if (inputFile != null) inputFile.close();     
	             if (remoteFile != null) emailFS.close(remoteFile);    
	             return true;
			}
			catch (Exception e2) {
				e2.printStackTrace();
			}
		}
		return false;
	}
*/

}
